package cn.elane.kibana.demo.global.util;


import cn.elane.kibana.demo.global.model.NameBean;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;

public class HttpUtil {

	/***
	 * post
	 * @param address    链接地址
	 * @param param        参数：{\"key\":\"value\"}
	 * @return String    结果
	 */
	public static String post(String address, String param) {
		try {
//			System.out.println(URLDecoder.decode(address, "UTF-8"));
			URL url = new URL(URLDecoder.decode(address, "UTF-8"));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
			connection.connect();
			//POST请求
			if (param != null) {
				DataOutputStream out = new DataOutputStream(connection.getOutputStream());
				String soapRequestJson = param;
				out.write(soapRequestJson.getBytes("UTF-8"));
				out.flush();
			}
			//读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes());
				sb.append(lines);
			}
			reader.close();
			connection.disconnect();
			return sb.toString();
		} catch (Exception e) {
//            e.printStackTrace();
			return "";
		}
	}

	/***
	 * get
	 * @param address    链接地址
	 * @param
	 * @return String    结果
	 */
	public static String get(String address) {
		try {
			URL url = new URL(address);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true); // 设置该连接是可以输出的
			connection.setRequestMethod("GET"); // 设置请求方式
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			connection.connect();
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			StringBuilder result = new StringBuilder();
			String line = null;
			while ((line = br.readLine()) != null) { // 读取数据
				line = new String(line.getBytes());
				result.append(line);
			}
			connection.disconnect();
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	//TODO: 根据param组件 key=val&key1=val1
	/**
	 * get
	 *
	 * @param address 链接地址
	 * @param
	 * @return String    结果
	 */
	public static String get(String address, NameBean param) {
		try {
			URL url = new URL(address);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true); // 设置该连接是可以输出的
			connection.setRequestMethod("GET"); // 设置请求方式
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			connection.connect();
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			StringBuilder result = new StringBuilder();
			String line = null;
			while ((line = br.readLine()) != null) { // 读取数据
				line = new String(line.getBytes());
				result.append(line);
			}
			connection.disconnect();
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
}
